package com.example.a00;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class historyData extends AppCompatActivity{

    private final String TAG = "historydata";
    private Spinner id_sp;
    private Spinner data_sp;

    //    String from[] = new String[]{"userid", "zd_id", "data", "temperature", "comgas", "flame", "shake", "light"};
    String[] from_temperature = new String[]{"time", "temperature"};
    String[] from_comgas = new String[]{"time", "comgas"};
    String[] from_flame = new String[]{"time", "flame"};
    String [] from_shake= new String[]{"time", "shake"};
    String [] from_light= new String[]{"time", "light"};

    int[] to = new int[]{R.id.sql_time, R.id.sql_data};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data_display);

        DBOpenHelper dbsqLiteOpenHelper = new DBOpenHelper(historyData.this,"users.db",null,1);
        SQLiteDatabase db = dbsqLiteOpenHelper.getWritableDatabase();

        Button button_delete = findViewById(R.id.button_clear);

        id_sp = findViewById(R.id.spinner_id);
        data_sp = findViewById(R.id.spinner_data);
        TextView data_sql = findViewById(R.id.textView_data_sql);

        id_sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                updateListView();
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        data_sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                data_sql.setText(data_sp.getSelectedItem().toString());
                updateListView();
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });


        button_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                db.delete("user",null,null);
            }//删除表中所有数据
        });

    }

    private void updateListView() {
        String selectedItem_id = (String)id_sp.getSelectedItem();
        String selectedItem_data = (String)data_sp.getSelectedItem();

        DBOpenHelper dbsqLiteOpenHelper = new DBOpenHelper(historyData.this,"users.db",null,1);
        SQLiteDatabase db = dbsqLiteOpenHelper.getWritableDatabase();

        Cursor cursor_temperature_id1 = db.query("user", new String[]{"_id","time","temperature"}, "_id = ?", new String[]{"1"}, null, null, null);
        Cursor cursor_comgas_id1 = db.query("user", new String[]{"_id","time","comgas"}, "_id = ?", new String[]{"1"}, null, null, null);
        Cursor cursor_flame_id1 = db.query("user", new String[]{"_id","time","flame"}, "_id = ?", new String[]{"1"}, null, null, null);
        Cursor cursor_shake_id1 = db.query("user", new String[]{"_id","time","shake"}, "_id = ?", new String[]{"1"}, null, null, null);
        Cursor cursor_light_id1 = db.query("user", new String[]{"_id","time","light"}, "_id = ?", new String[]{"1"}, null, null, null);

        Cursor cursor_temperature_id2 = db.query("user", new String[]{"_id","time","temperature"}, "_id = ?", new String[]{"2"}, null, null, null);
        Cursor cursor_comgas_id2 = db.query("user", new String[]{"_id","time","comgas"}, "_id = ?", new String[]{"2"}, null, null, null);
        Cursor cursor_flame_id2 = db.query("user", new String[]{"_id","time","flame"}, "_id = ?", new String[]{"2"}, null, null, null);
        Cursor cursor_shake_id2 = db.query("user", new String[]{"_id","time","shake"}, "_id = ?", new String[]{"2"}, null, null, null);
        Cursor cursor_light_id2 = db.query("user", new String[]{"_id","time","light"}, "_id = ?", new String[]{"2"}, null, null, null);

        Cursor cursor_temperature_id3 = db.query("user", new String[]{"_id","time","temperature"}, "_id = ?", new String[]{"3"}, null, null, null);
        Cursor cursor_comgas_id3 = db.query("user", new String[]{"_id","time","comgas"}, "_id = ?", new String[]{"3"}, null, null, null);
        Cursor cursor_flame_id3 = db.query("user", new String[]{"_id","time","flame"}, "_id = ?", new String[]{"3"}, null, null, null);
        Cursor cursor_shake_id3 = db.query("user", new String[]{"_id","time","shake"}, "_id = ?", new String[]{"3"}, null, null, null);
        Cursor cursor_light_id3 = db.query("user", new String[]{"_id","time","light"}, "_id = ?", new String[]{"3"}, null, null, null);

        SimpleCursorAdapter adapter_temperature_id1 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_temperature_id1, from_temperature, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_comgas_id1 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_comgas_id1, from_comgas, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_flame_id1 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_flame_id1, from_flame, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_shake_id1 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_shake_id1, from_shake, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_light_id1 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_light_id1, from_light, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        SimpleCursorAdapter adapter_temperature_id2 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_temperature_id2, from_temperature, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_comgas_id2 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_comgas_id2, from_comgas, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_flame_id2 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_flame_id2,from_flame, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_shake_id2 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_shake_id2, from_shake, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_light_id2 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_light_id2, from_light, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        SimpleCursorAdapter adapter_temperature_id3 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_temperature_id3, from_temperature, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_comgas_id3 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_comgas_id3, from_comgas, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_flame_id3 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_flame_id3, from_flame, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_shake_id3 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_shake_id3, from_shake, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        SimpleCursorAdapter adapter_light_id3 = new SimpleCursorAdapter(this, R.layout.historydata, cursor_light_id3, from_light, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        ListView list_view = findViewById(R.id.list_view);

        if (selectedItem_id.equals("车间一")){
            if (selectedItem_data.equals("温度")){
                list_view.setAdapter(adapter_temperature_id1);
            }
            if (selectedItem_data.equals("气体")){
                list_view.setAdapter(adapter_comgas_id1);
            }
            if (selectedItem_data.equals("火焰")){
                list_view.setAdapter(adapter_flame_id1);
            }
            if (selectedItem_data.equals("震动")){
                list_view.setAdapter(adapter_shake_id1);
            }
            if (selectedItem_data.equals("光照")){
                list_view.setAdapter(adapter_light_id1);
            }

        }
        if (selectedItem_id.equals("车间二")){
            if (selectedItem_data.equals("温度")){
                list_view.setAdapter(adapter_temperature_id2);
            }
            if (selectedItem_data.equals("气体")){
                list_view.setAdapter(adapter_comgas_id2);
            }
            if (selectedItem_data.equals("火焰")){
                list_view.setAdapter(adapter_flame_id2);
            }
            if (selectedItem_data.equals("震动")){
                list_view.setAdapter(adapter_shake_id2);
            }
            if (selectedItem_data.equals("光照")){
                list_view.setAdapter(adapter_light_id2);
            }

        }
        if (selectedItem_id.equals("车间三")){
            if (selectedItem_data.equals("温度")){
                list_view.setAdapter(adapter_temperature_id3);
            }
            if (selectedItem_data.equals("气体")){
                list_view.setAdapter(adapter_comgas_id3);
            }
            if (selectedItem_data.equals("火焰")){
                list_view.setAdapter(adapter_flame_id3);
            }
            if (selectedItem_data.equals("震动")){
                list_view.setAdapter(adapter_shake_id3);
            }
            if (selectedItem_data.equals("光照")){
                list_view.setAdapter(adapter_light_id3);
            }

        }
    }
}
